package com.appstore.model;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import net.sf.json.JSONArray;

/**
 * Application is mapped to application table.
 * 
 * @author zhbhun
 * 
 */
public class Application {

	private Integer id;
	private String name;
	private String size;
	private String icon;
	private Timestamp createdTime;
	private Timestamp updatedTime;
	private Boolean deleted;
	private String description;
	private String screenShot;
	private Integer like;
	private Integer dislike;
	private String twoDimentCode;
	private String downloadUrl;
	private List<String> screenList;
	private Integer downloadCount;
	private String version;

	public Application() {
		this.name = "";
		this.size = "0M";
		this.icon = "";
		this.createdTime = new Timestamp(new Date().getTime());
		this.updatedTime = this.createdTime;
		this.deleted = false;
		this.description = "";
		this.screenShot = "[]";
		this.like = 0;
		this.dislike = 0;
		this.twoDimentCode = "";
		this.downloadUrl = "";
		this.screenList = null;
		this.downloadCount = 0;
		this.version = "";
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSize() {
		return size;
	}

	public void setSize(String size) {
		this.size = size;
	}

	public String getIcon() {
		return icon;
	}

	public void setIcon(String icon) {
		this.icon = icon;
	}

	public Timestamp getCreatedTime() {
		return createdTime;
	}

	public void setCreatedTime(Timestamp createdTime) {
		this.createdTime = createdTime;
	}

	public Timestamp getUpdatedTime() {
		return updatedTime;
	}

	public void setUpdatedTime(Timestamp updatedTime) {
		this.updatedTime = updatedTime;
	}

	public Boolean getDeleted() {
		return deleted;
	}

	public void setDeleted(Boolean deleted) {
		this.deleted = deleted;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getScreenShot() {
		return screenShot;
	}

	public void setScreenShot(String screenShot) {
		this.screenShot = screenShot;
		if (this.screenShot == null || "".equals(this.screenShot)) {
			this.screenShot = "[]";
		}
	}

	public Integer getLike() {
		return like;
	}

	public void setLike(Integer like) {
		this.like = like;
	}

	public Integer getDislike() {
		return dislike;
	}

	public void setDislike(Integer dislike) {
		this.dislike = dislike;
	}

	public String getTwoDimentCode() {
		return twoDimentCode;
	}

	public void setTwoDimentCode(String twoDimentCode) {
		this.twoDimentCode = twoDimentCode;
	}

	public String getDownloadUrl() {
		return downloadUrl;
	}

	public void setDownloadUrl(String downloadUrl) {
		this.downloadUrl = downloadUrl;
	}

	public List<String> getScreenList() {
		return screenList;
	}

	public void setScreenList(List<String> screenList) {
		this.screenList = screenList;
	}

	public Integer getDownloadCount() {
		return downloadCount;
	}

	public void setDownloadCount(Integer downloadCount) {
		this.downloadCount = downloadCount;
	}

	public String getVersion() {
		return version;
	}

	public void setVersion(String version) {
		this.version = version;
	}

	/**
	 * create the screenshot array from JSON string
	 * 
	 * @param screenShots
	 */
	public void setScreenList(String screenShots) {
		JSONArray array = JSONArray.fromObject(screenShots);
		this.screenList = new ArrayList<String>();
		for (int i = 0; i < array.size(); i++) {
			this.screenList.add(array.getString(i));
		}
	}

	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("Application [id=");
		builder.append(id);
		builder.append(", name=");
		builder.append(name);
		builder.append(", size=");
		builder.append(size);
		builder.append(", icon=");
		builder.append(icon);
		builder.append(", createdTime=");
		builder.append(createdTime);
		builder.append(", updatedTime=");
		builder.append(updatedTime);
		builder.append(", deleted=");
		builder.append(deleted);
		builder.append(", description=");
		builder.append(description);
		builder.append(", screenShot=");
		builder.append(screenShot);
		builder.append(", like=");
		builder.append(like);
		builder.append(", dislike=");
		builder.append(dislike);
		builder.append(", twoDimentCode=");
		builder.append(twoDimentCode);
		builder.append(", downloadUrl=");
		builder.append(downloadUrl);
		builder.append(", downloadCount=");
		builder.append(downloadCount);
		builder.append(", version=");
		builder.append(version);
		builder.append("]");
		return builder.toString();
	}

}
